﻿@page "/demo/weather"
@inherits BaseComponent
@attribute [StreamRendering]
@attribute [Menu(AppConstant.Demo, "天气", "cloud", 3)]
@inject HttpClient Http

<PageTitle>天气</PageTitle>

<TablePage Model="model" />

@code {
    private TableModel<WeatherForecast> model;

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        model = new TableModel<WeatherForecast>(this, TableColumnMode.Property);
        model.ShowPager = true;
        model.Name = "天气";
        model.OnQuery = OnQueryWeatherForecastsAsync;
        model.Toolbar.AddAction(nameof(New));
    }

    public void New()
    {
        model.NewForm(d =>
        {
            UI.Alert($"Summary={d.Summary}");
            return Known.Result.SuccessAsync("");
        });
    }

    private async Task<PagingResult<WeatherForecast>> OnQueryWeatherForecastsAsync(PagingCriteria criteria)
    {
        await Task.Delay(500);

        // var startDate = DateOnly.FromDateTime(DateTime.Now);
        // var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
        // var forecasts = Enumerable.Range(1, criteria.PageSize).Select(index => new WeatherForecast
        // {
        //     Date = startDate.AddDays(index),
        //     TemperatureC = Random.Shared.Next(-20, 55),
        //     Summary = summaries[Random.Shared.Next(summaries.Length)]
        // }).ToList();

        var forecasts = await Http.GetFromJsonAsync<List<WeatherForecast>>("sample-data/weather.json");
        return new PagingResult<WeatherForecast>(100, forecasts);
    }

    public class WeatherForecast
    {
        [Column(IsQuery = true)]
        [DisplayName("日期")]
        public DateOnly Date { get; set; }
        public int TemperatureC { get; set; }
        public string Summary { get; set; }
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    }
}